package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.PhoneCheckCode;
import com.qian.api.ddj.service.IPhoneCheckCodeService;
import com.qian.service.ddj.dao.PhoneCheckCodeRepository;
import com.qian.service.ddj.entity.DdjPhoneCheckCode;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PhoneCheckCodeServiceImpl extends BaseServiceImpl<PhoneCheckCode,DdjPhoneCheckCode,Integer> implements IPhoneCheckCodeService {
    @Autowired
    private PhoneCheckCodeRepository dao;

    @Override
    public PhoneCheckCode save(PhoneCheckCode phoneCheckCode){
        if (phoneCheckCode.isNew()){
           phoneCheckCode = simpleSave(phoneCheckCode);
        }else{
            DdjPhoneCheckCode ddjPhoneCheckCode = dao.findOne(phoneCheckCode.getId());
            if (isNotEmpty(phoneCheckCode.getCheckCode())){
                ddjPhoneCheckCode.setCheckCode(phoneCheckCode.getCheckCode());
            }
            if (isNotEmpty(phoneCheckCode.getPhoneNumber())){
                ddjPhoneCheckCode.setPhoneNumber(phoneCheckCode.getPhoneNumber());
            }
            if (isNotEmpty(phoneCheckCode.getUserName())){
                ddjPhoneCheckCode.setUsername(phoneCheckCode.getUserName());
            }
            if (isNotEmpty(phoneCheckCode.getCreateDate())){
                ddjPhoneCheckCode.setCreateDate(phoneCheckCode.getCreateDate());
            }
            phoneCheckCode = entity2model(dao.save(ddjPhoneCheckCode));
        }
        return phoneCheckCode;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjPhoneCheckCode model2Entity(PhoneCheckCode model) {
        DdjPhoneCheckCode ddjPhoneCheckCode = null;
        if (isNotEmpty(model)){
            ddjPhoneCheckCode = new DdjPhoneCheckCode();
            ddjPhoneCheckCode.setCheckCode(model.getCheckCode());
            ddjPhoneCheckCode.setPhoneNumber(model.getPhoneNumber());
            ddjPhoneCheckCode.setUsername(model.getUserName());
            ddjPhoneCheckCode.setId(model.getId());
            ddjPhoneCheckCode.setCreateDate(model.getCreateDate());
        }
        return ddjPhoneCheckCode;
    }

    @Override
    protected PhoneCheckCode entity2model(DdjPhoneCheckCode entity) {
        PhoneCheckCode model = null;
        if (isNotEmpty(entity)){
            model = new PhoneCheckCode();
            model.setCheckCode(entity.getCheckCode());
            model.setPhoneNumber(entity.getPhoneNumber());
            model.setUserName(entity.getUsername());
            model.setId(entity.getId());
            model.setCreateDate(entity.getCreateDate());
        }
        return model;
    }
}
